package com.mes.common.util;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.bstek.bdf2.core.context.ContextHolder;
import com.mes.basicdata.dao.EmployeeDao;
import com.mes.basicdata.domain.Employee;
import com.mes.common.spring.SpringUtils;
import com.mes.system.domain.Message;
import com.mes.system.domain.MessageContent;
import com.mes.system.manager.iface.IMessageManager;

@Component
public class SysMessageUtil {

	// @Autowired
	// private ConfigInfo configInfo;
	// @Autowired
	// private EmployeeDao employeeDao;
	// @Autowired
	// private IMessageManager messageManager;
	private static final Log log = LogFactory.getLog(SysMessageUtil.class);

	public static void sendMessage(String destEmployeeUid, String content) {
		try {
			if (destEmployeeUid == null || content == null)
				return;

			Message message = new Message();
			String fromE = SysUtil.getSessionEmployeeUid();
			if (fromE == null)
				fromE = "admin";
			message.setFromEmployeeUid(fromE);
			message.setSendTime(new Date());
			message.setToEmployeeUid(destEmployeeUid);

			MessageContent messageContent = new MessageContent();
			messageContent.setUuid(SysUtil.getUUID());
			messageContent.setTopic(content);
			messageContent.setContent(content);

			// message.setMessageContentUid(messageContent.getUuid());
			message.setMessageContent(messageContent);
			// messageContentManager.add(messageContent);

			IMessageManager messageManager = (IMessageManager) SpringUtils.getBean("messageManager");
			messageManager.sendMessage(message);

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	// 采用其它线程发，避免阻塞主线程
	public static void sendDeviceMessage(String deviceUid, String content, boolean needAlert) {
		try {
//			DeviceMessageSender deviceMessageSender = new DeviceMessageSender(deviceUid, content, needAlert);
//			SysUtil.getExecutorService().submit(deviceMessageSender);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
